package nc.impl.wsdl.pub;

import com.alibaba.fastjson.JSONObject;
import nc.bs.trade.business.HYPubBO;
import nc.bs.wsdl.pub.DataInfoHelper;
import nc.itf.wsdl.pub.IWSDLLogger;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.AppContext;
import nc.vo.wsdl.pub.WSDLLoggerVO;

/**
 * 日志接口实现类
 *
 * @author lwj
 * @date 2022-6-27
 *
 */
public class WSDLLoggerImpl implements IWSDLLogger
{
	private HYPubBO hyPubBO = null;

	private HYPubBO getHYPubBO()
	{
		if (hyPubBO == null)
		{
			hyPubBO = new HYPubBO();
		}
		return hyPubBO;
	}

	/**
	 * 独立事务保存日志
	 *
	 * @param serverTime
	 * @param requestJson
	 * @param responseJson
	 * @throws BusinessException
	 */
	@Override
	public void saveLogger_RequiresNew(UFDateTime serverTime, JSONObject requestJson, JSONObject responseJson) throws BusinessException
	{
		if (requestJson == null)
		{
			return;// 请求报文为空则不记录日志
		}

		/**
		 * 计算耗时
		 */
		long startTime = serverTime.getMillis();// 开始时间
		long endTime = AppContext.getInstance().getServerTime().getMillis();// 结束时间
		float seconds = (float) (endTime - startTime) / 1000;// 耗时(s)

		/**
		 * 请求报文
		 */
		String action = DataInfoHelper.formatString(requestJson.get("action"));// 动作
		String datatype = "FCT";
		String srcsystem = "YHT";
		String goalsystem = "HOPG";

//		String datatype = DataInfoHelper.formatString(requestJson.get("datatype"));// 数据类型
//		String srcsystem = DataInfoHelper.formatString(requestJson.get("srcsystem"));// 来源系统
//		String goalsystem = DataInfoHelper.formatString(requestJson.get("goalsystem"));// 目的系统
//		String srcid = DataInfoHelper.formatString(requestJson.get("srcid"));// 来源系统唯一标识

		/**
		 * 响应报文
		 */
		String returnstatus = "N";// 处理状态
		if (responseJson == null)
		{
			returnstatus = "N";
		}
		else if (responseJson.get("status") != null)
		{
			Object status = responseJson.get("status");// 状态
			if (status == null)
			{
				returnstatus = "N";
			}
			else if (status instanceof Boolean)
			{
				returnstatus = (Boolean) status ? "Y" : "N";// 处理状态
			}
			else if (status instanceof String)
			{
				/**
				 * 针对OA返回报文
				 */
				String statusOA = responseJson.getString("status");// 状态：1=数据插入或更新完成无异常,2=数据插入或更新完成但有异常,3=插入或更新数据有异常,4=系统接口有异常
				if (statusOA.equals("1") || statusOA.equals("2"))
				{
					returnstatus = "Y";
				}
				else
				{
					returnstatus = "N";
				}
			}
			else
			{
				returnstatus = "N";
			}
		}

		/**
		 * 封装日志VO
		 */
		WSDLLoggerVO loggerVO = new WSDLLoggerVO();
		loggerVO.setStatus(VOStatus.NEW);
		loggerVO.setDr(0);
		loggerVO.setDatatype(datatype);// 数据类型
		loggerVO.setAction(action);// 动作
		loggerVO.setGoalsystem(goalsystem);// 目的系统
		loggerVO.setSrcsystem(srcsystem);// 来源系统
		loggerVO.setSrcid("");// 来源系统主键
		loggerVO.setReceivetime(serverTime);// 接收时间
		loggerVO.setSeconds(seconds);// 处理时间
		loggerVO.setReturnstatus(returnstatus);// 处理状态
		loggerVO.setRequest(requestJson);// 请求报文
		loggerVO.setResponse(responseJson);// 响应报文
		this.getHYPubBO().insert(loggerVO);
	}
}
